using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using Microsoft.TeamFoundation.VersionControl.Client;
using System.IO;

namespace Ltf.Remoting
{
	[Serializable]
	public class LizardFileInfo
	{
		public void FillFromExtendedItem(ExtendedItem extendedItem)
		{
			this.LockOwner = extendedItem.LockOwner;
			this.LockStatus = extendedItem.LockStatus.ToString();
			this.ChangeType = extendedItem.ChangeType.ToString();
			this.LocalVersion = extendedItem.VersionLocal;
			this.ServerVersion = extendedItem.VersionLatest;
			this.ServerPath = extendedItem.SourceServerItem;
			this.HasOtherPendingChange = extendedItem.HasOtherPendingChange;
			this.LocalItem = extendedItem.LocalItem;
		}

		public void CalculateOverlayStatus(string tfsUserName)
		{
			LizardOverLayStatus olt = LizardOverLayStatus.None;
			if (this.Status == LizardFileStatus.Same && this.LockStatus == "None" && this.ChangeType.IndexOf("Edit") >= 0 && this.LocalVersion == this.ServerVersion)
				olt = LizardOverLayStatus.CheckedOut;
			if (this.Status == LizardFileStatus.Same && this.LockStatus == "None" && this.ChangeType.IndexOf("Edit") >= 0 && this.LocalVersion != this.ServerVersion)
				olt = LizardOverLayStatus.OutOfDate;
			if (this.Status == LizardFileStatus.Same && this.LockStatus == "None" && this.ChangeType == "None" && this.LocalVersion == this.ServerVersion)
			{
				if (readOnly)
					olt = LizardOverLayStatus.ReadOnly;
				else
					olt = LizardOverLayStatus.CheckedInWritable;
			}
			if (this.Status == LizardFileStatus.Different && this.LockStatus == "None" && this.ChangeType.IndexOf("Edit") >= 0 && this.LocalVersion == this.ServerVersion)
				olt = LizardOverLayStatus.CheckedOut;
			if (Path.GetFileName(this.ServerPath) != Path.GetFileName(this.LocalItem))
				olt = LizardOverLayStatus.Other; // renamed!
			if (this.LockStatus == "None" && this.ChangeType == "None" && this.LocalVersion != this.ServerVersion)
				olt = LizardOverLayStatus.OutOfDate;
			if (this.Status == LizardFileStatus.Same && this.LockStatus != "None")
				olt = LizardOverLayStatus.Locked;
			if (this.Status == LizardFileStatus.Different && this.LocalVersion == this.ServerVersion)
			{
				if (this.ChangeType.ToString() == "None")
					olt = LizardOverLayStatus.CheckedInDifferent;
				else
					olt = LizardOverLayStatus.Different;
			}
			if (this.Status == LizardFileStatus.Different && this.ChangeType.IndexOf("Edit") >= 0 && this.LocalVersion != this.ServerVersion)
				olt = LizardOverLayStatus.Conflict;
			if (this.Status == LizardFileStatus.Added)
				olt = LizardOverLayStatus.Add;
			if (this.Status == LizardFileStatus.Deleted)
				olt = LizardOverLayStatus.Deleted;
			if (this.ChangeType.IndexOf("Branch") >= 0 || this.ChangeType.IndexOf("Rename") >= 0)
				olt = LizardOverLayStatus.Different;

			if (olt == LizardOverLayStatus.Different && (this.ChangeType.IndexOf("Branch") >= 0))
				olt = LizardOverLayStatus.Add;
			if (this.LockStatus == "None" && this.ChangeType == "None" && this.LocalVersion == this.ServerVersion)
			{
				if (readOnly)
					olt = LizardOverLayStatus.ReadOnly;
				else
					olt = LizardOverLayStatus.CheckedInWritable;
			}
			if ((this.LockStatus == "None" || this.LockOwner != tfsUserName) && this.ChangeType == "None" && this.Status == LizardFileStatus.Different)
			{
				olt = LizardOverLayStatus.CheckedInDifferent;
			}

			this.OverlayType = olt;
		}

		private LizardFileStatus status;

		private LizardOverLayStatus overlayType;

		public LizardOverLayStatus OverlayType
		{
			get { return overlayType; }
			set { overlayType = value; }
		}

		public LizardFileStatus Status
		{
			get { return status; }
			set { status = value; }
		}

		private int localVersion;

		public int LocalVersion
		{
			get { return localVersion; }
			set { localVersion = value; }
		}

		private int serverVersion;

		public int ServerVersion
		{
			get { return serverVersion; }
			set { serverVersion = value; }
		}

		private string lockowner;

		public string LockOwner
		{
			get { return lockowner; }
			set { lockowner = value; }
		}

		private string lockStatus;

		public string LockStatus
		{
			get { return lockStatus; }
			set { lockStatus = value; }
		}


		private string changeType;

		public string ChangeType
		{
			get { return changeType; }
			set { changeType = value; }
		}

		private int itemId;

		public int Itemid
		{
			get { return itemId; }
			set { itemId = value; }
		}

		private string serverPath;

		public string ServerPath
		{
			get { return serverPath; }
			set { serverPath = value; }
		}

		private string localItem;

		public string LocalItem
		{
			get { return localItem; }
			set { localItem = value; }
		}


		private bool hasOtherPendingChange;

		public bool HasOtherPendingChange
		{
			get { return hasOtherPendingChange; }
			set { hasOtherPendingChange = value; }
		}
		private string otherUsers;

		public string OtherUsers
		{
			get { return otherUsers; }
			set { otherUsers = value; }
		}
	
		private int localVersionChecksum=0;

		public int LocalVersionChecksum
		{
			get { return localVersionChecksum; }
			set { localVersionChecksum = value; }
		}

		private long localVersionLength=0;

		public long LocalVersionLength
		{
			get { return localVersionLength; }
			set { localVersionLength = value; }
		}
		
		private int workspaceVersionChecksum=0;

		public int WorkspaceVersionChecksum
		{
			get { return workspaceVersionChecksum; }
			set { workspaceVersionChecksum = value; }
		}

		private DateTime workspaceVersionChecksumDate=new DateTime(1900,1,1);

		public DateTime WorkspaceVersionChecksumDate
		{
			get { return workspaceVersionChecksumDate; }
			set { workspaceVersionChecksumDate = value; }
		}

		private DateTime fileCreatedDate = new DateTime(1900, 1, 1);

		public DateTime FileCreatedDate
		{
			get { return fileCreatedDate; }
			set { fileCreatedDate = value; }
		}

		private DateTime fileWrittenDate = new DateTime(1900, 1, 1);

		public DateTime FileWrittenDate
		{
			get { return fileWrittenDate; }
			set { fileWrittenDate = value; }
		}

		private bool readOnly = false;

		public bool ReadOnly
		{
			get { return readOnly; }
			set { readOnly = value; }
		}

	}
}
